﻿using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

//------------------------------------------------------------
// shaco Framework
// Copyright © 2017-2021 chang.liu All rights reserved.
// Feedback: 449612236@qq.com
//------------------------------------------------------------

namespace shacoEditor
{
    [CustomEditor(typeof(shaco.ActionDelegate))]
    public class ActionDelegateInspector : Editor
    {
        public override void OnInspectorGUI()
        {
            Undo.RecordObject(target, target.GetType().FullName);

            this.Repaint();
            base.OnInspectorGUI();

            var allActions = shaco.GameHelper.action.GetAllActions();
            foreach (var iter in allActions)
            {
                GUILayout.BeginVertical("box");
                {
                    if (DrawHeader(iter.Key))
                    {
                        for (int i = 0; i < iter.Value.Count; ++i)
                        {
                            var action = iter.Value[i];

                            EditorGUI.BeginDisabledGroup(true);
                            {
                                GUILayout.BeginHorizontal();
                                {
									GUILayout.Label("Eplapse", GUILayout.ExpandWidth(false));
									GUILayout.Label(action.elapsed.ToString(), GUILayout.ExpandWidth(false));

									GUILayout.Label("Duration", GUILayout.ExpandWidth(false));
									GUILayout.Label(action.duration.ToString(), GUILayout.ExpandWidth(false));

									GUILayout.Label("Paused", GUILayout.ExpandWidth(false));
                                    GUILayout.Toggle(action.isPaused, string.Empty, GUILayout.ExpandWidth(false));
                                }
                                GUILayout.EndHorizontal();
                            }
							EditorGUI.EndDisabledGroup();

                            if (!action.isRemoved)
                            {
                                GUILayout.BeginHorizontal();
                                {
                                    GUILayout.Space(24);
                                    GUILayout.Label(action.actionName);

                                    if (null != action.stackLocationRunAction && action.stackLocationRunAction.HasStack() && GUILayout.Button("Location", GUILayout.ExpandWidth(false)))
                                    {
                                        shaco.Log.Info(action.stackLocationRunAction.GetTotalStackInformation());
                                        EditorHelper.OpenAsset(action.stackLocationRunAction.GetStackInformation(), action.stackLocationRunAction.GetStackLine());
                                    }
                                }
                                GUILayout.EndHorizontal();
                            }
                        }
                    }
                }
                GUILayout.EndVertical();
            }
        }

        private bool DrawHeader(GameObject target)
        {
            bool isOpened = shaco.GameHelper.datasave.ReadBool(target.name, true);

            GUILayout.BeginHorizontal();
            {
                if (GUILayout.Button(isOpened ? "-" : "+", GUILayout.Width(20)))
                {
                    isOpened = !isOpened;
                    shaco.GameHelper.datasave.WriteBool(target.name, isOpened);
                }

                EditorGUI.BeginDisabledGroup(true);
                {
                    EditorGUILayout.ObjectField(target, typeof(GameObject), true);
                }
                EditorGUI.EndDisabledGroup();
            }
            GUILayout.EndHorizontal();

            return isOpened;
        }
    }
}